﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ReadFilesFromDirectory
{
    public partial class NewSession : Form
    {
        private Credentials credentials;
        public NewSession()
        {
            InitializeComponent();
            setUpDefaultCredentials();
        }

        private void cancelButtonClicked(object sender, EventArgs e)
        {
            setUpServerFormWhenCancelClicked();
            enableOwner();
        }

        private void okButtonClicked(object sender, EventArgs e)
        {
            closeNewSessionForm();
        }

        private void closeNewSessionForm()
        {
            setTrusteeUserName();
            setTrusteePasswd();
            setInvestorUserName();
            setInvestorPasswd();
            createFolderForThisSession();
            enableOwner();
        }

        private void setUpServerFormWhenCancelClicked()
        {
            ServerMainForm form = (ServerMainForm)this.Owner;

            form.button_startsession.Enabled = true;
            form.startSessionToolStripMenu.Enabled = true;

            form.button_stopServer.Enabled = true;
            form.stopTheSessionToolStripMenuItem.Enabled = true;

            form.button_startServer.Enabled = false;
            form.startServerToolStripMenuItem.Enabled = false;
        }

        private void enableOwner()
        {
            this.Owner.Enabled = true;
            this.Dispose();
        }

        private void createFolderForThisSession()
        {
            string activeDir = @"C:\\Sessions";
            System.IO.Directory.CreateDirectory(activeDir);

            //Create a new subfolder under the current active folder
            string newSessionDir = System.IO.Path.Combine(activeDir, textBox1.Text);

            // Create the subfolder
            System.IO.Directory.CreateDirectory(newSessionDir);

            if (textBox1.Text.Length > 0)
            {
                credentials.FolderName = "C:\\Sessions\\" + textBox1.Text;
            }
            else
            {
                DateTime dT = DateTime.Now;
                string s = (dT.GetDateTimeFormats())[5];
                credentials.FolderName = "C:\\Sessions\\" + s + "-session-number";
            }
        }

        private void setInvestorPasswd()
        {
            if (textBox_investorPasswd.Text.Length > 0)
                credentials.InvestorPasswd = textBox_investorPasswd.Text;
            else
                credentials.InvestorPasswd = "investor";
        }

        private void setInvestorUserName()
        {
            if (textBox_InvestorUserName.Text.Length > 0)
                credentials.InvestorUserName = textBox_InvestorUserName.Text;
            else
                credentials.InvestorUserName = "investor";
        }

        private void setTrusteePasswd()
        {
            if (textBox_trusteePasswd.Text.Length > 0)
                credentials.TrusteePasswd = textBox_trusteePasswd.Text;
            else
                credentials.TrusteePasswd = "trustee";
        }

        private void setTrusteeUserName()
        {
            if (textBox_trusteeUserName.Text.Length > 0)
                credentials.TrusteeUserName = textBox_trusteeUserName.Text;
            else
                credentials.TrusteeUserName = "trustee";
        }

        private void setUpDefaultCredentials()
        {
            DateTime dT = DateTime.Now;
            string s = (dT.GetDateTimeFormats())[5];
            textBox1.Text = s + "-session-number";
            credentials = Credentials.Instance;
        }
    }
}
